package game;

import java.util.ArrayList;

public class Checkers {
	private Renderer renderer = new Renderer2D(this);
	private ArrayList<Piece> pieces;
	private Piece selectedPiece;
	private int selectedX;
	private int selectedY;
	private boolean whiteTurn;

	public Piece getSelectedPiece() {
		return selectedPiece;
	}

	public void setSelectedPiece(Piece selectedPiece) {
		this.selectedPiece = selectedPiece;
	}

	public static final int WIDTH = 8;
	public static final int HEIGHT = 8;

	public Checkers() {
		pieces = new ArrayList<Piece>(24);
	}

	public void init() {
		pieces.clear();

		pieces.add(new Piece(1, 0, true, false));
		pieces.add(new Piece(3, 0, true, false));
		pieces.add(new Piece(5, 0, true, false));
		pieces.add(new Piece(7, 0, true, false));

		pieces.add(new Piece(0, 1, true, false));
		pieces.add(new Piece(2, 1, true, false));
		pieces.add(new Piece(4, 1, true, false));
		pieces.add(new Piece(6, 1, true, false));
		/*
		 * pieces.add(new Piece(1, 2, true, false)); pieces.add(new Piece(3, 2,
		 * true, false)); pieces.add(new Piece(5, 2, true, false));
		 * pieces.add(new Piece(7, 2, true, false));
		 * 
		 * pieces.add(new Piece(0, 5, false, false)); pieces.add(new Piece(2, 5,
		 * false, false)); pieces.add(new Piece(4, 5, false, false));
		 * pieces.add(new Piece(6, 5, false, false));
		 */
		pieces.add(new Piece(1, 6, false, false));
		pieces.add(new Piece(3, 6, false, false));
		pieces.add(new Piece(5, 6, false, false));
		pieces.add(new Piece(7, 6, false, false));

		pieces.add(new Piece(0, 7, false, false));
		pieces.add(new Piece(2, 7, false, false));
		pieces.add(new Piece(4, 7, false, false));
		pieces.add(new Piece(6, 7, false, false));

		whiteTurn = true;

		selectedPiece = null;
		selectedX = -1;
		selectedY = -1;

		renderer.init();
		loop();
	}

	public void loop() {
		boolean running = true;
		long lastDraw = 0;
		while (running) {
			Piece temp = renderer.clickedPiece();
			if (temp != null && temp.isWhite() == whiteTurn)
				selectedPiece = temp;

			if (temp == null) {
				if (renderer.getClickedX() != -1) {
					selectedX = renderer.getClickedX();
					selectedY = renderer.getClickedY();
				}
			}

			if (whiteTurn) {
				if (selectedX != -1 && selectedPiece != null) {
						boolean move = false;
						Piece victim1 = getPiece(selectedPiece.getX() + 1, selectedPiece.getY() + 1);
						Piece victim2 = getPiece(selectedPiece.getX() - 1, selectedPiece.getY() + 1);
						
					if (!selectedPiece.isKing()) {
						if (victim1 != null && victim1.isWhite() != selectedPiece.isWhite()) {
							if (selectedX == selectedPiece.getX() + 2 && selectedY == selectedPiece.getY() + 2) {
								move = true;
								pieces.remove(victim1);
							}
						}
						if (victim2 != null && victim2.isWhite() != selectedPiece.isWhite()) {
							if (selectedX == selectedPiece.getX() - 2 && selectedY == selectedPiece.getY() + 2) {
								move = true;
								pieces.remove(victim2);
							}
						}

						if (Math.abs(selectedPiece.getX() - selectedX) == 1 && selectedPiece.getY() - selectedY == -1) {
							move = true;
						}
					} else {
						//	kontrola pohybu damy
						if((selectedX+selectedY)==(selectedPiece.getX()+selectedPiece.getY()) || posunDama()){
							victim1 = kontrolaSkoku();
							
							if(victim1 != null){
								if(victim1 != selectedPiece){
									pieces.remove(victim1);
								}
								move = true;
							}
						}
					}
					
					if (move) {
						selectedPiece.setPos(selectedX, selectedY);
						if(selectedY == 7){ selectedPiece.setKing(true);}
						selectedX = -1;
						selectedY = -1;
						selectedPiece = null;
						whiteTurn = false;
					}
				}
			} else {
				if (selectedX != -1 && selectedPiece != null) {
						boolean move = false;
						Piece victim1 = getPiece(selectedPiece.getX() + 1, selectedPiece.getY() - 1);
						Piece victim2 = getPiece(selectedPiece.getX() - 1, selectedPiece.getY() - 1);
						
					if (!selectedPiece.isKing()) {
						if (victim1 != null && victim1.isWhite() != selectedPiece.isWhite()) {
							if (selectedX == selectedPiece.getX() + 2 && selectedY == selectedPiece.getY() - 2) {
								move = true;
								pieces.remove(victim1);
							}
						}
						if (victim2 != null && victim2.isWhite() != selectedPiece.isWhite()) {
							if (selectedX == selectedPiece.getX() - 2 && selectedY == selectedPiece.getY() - 2) {
								move = true;
								pieces.remove(victim2);
							}
						}
						if (!move)
							if (Math.abs(selectedPiece.getX() - selectedX) == 1 && selectedPiece.getY() - selectedY == 1) {
								move = true;
							}
					} else {
						//	kontrola pohybu damy
						if((selectedX+selectedY)==(selectedPiece.getX()+selectedPiece.getY()) || posunDama()){
							victim1 = kontrolaSkoku();
							
							if(victim1 != null){
								if(victim1 != selectedPiece){
									pieces.remove(victim1);
								}
								move = true;
							}
						}
					}
					
					if (move) {
						selectedPiece.setPos(selectedX, selectedY);
						if(selectedY == 0){ selectedPiece.setKing(true);}
						selectedX = -1;
						selectedY = -1;
						selectedPiece = null;
						whiteTurn = true;
					}
				}
			}

			if (System.currentTimeMillis() - lastDraw > 20) {
				renderer.draw();
				lastDraw = System.currentTimeMillis();
			}
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	// tu zistim ci dama moze ist na urceny bod alebo nie
	private boolean posunDama(){
		int rozdiel = selectedX - selectedPiece.getX();
		
		if((selectedPiece.getX()+rozdiel == selectedX) && (selectedPiece.getY()+rozdiel == selectedY)){
			return true;
		}
		return false;
	}
	
	//	kontrola ci dama vyhodila alebo nevyhodila niekoho
	private Piece kontrolaSkoku(){
		//	tieto dve premenne sluzia na urcenie smeru ci sa bude v danom smere pricitavat alebo odcitavat
		int smerX = 0;
		int smerY = 0;
		
		//	nastavenie premennych podla smeru ktorim sa posunula dama  
		if(selectedPiece.getX() < selectedX && selectedPiece.getY() > selectedY){
			smerX = 1;
			smerY = -1;
		}else if(selectedPiece.getX() > selectedX && selectedPiece.getY() < selectedY){
			smerX = -1;
			smerY = 1;
		}else if(selectedPiece.getX() > selectedX && selectedPiece.getY() > selectedY){
			smerX = -1;
			smerY = -1;
		}else if(selectedPiece.getX() < selectedX && selectedPiece.getY() < selectedY){
			smerX = 1;
			smerY = 1;
		}
		
		Piece p = null;
		boolean predosli = false;
		
		//	kontrola cesty damy ci sa tam nenachadza pesec
		for(int i = 1; i < Math.abs(selectedPiece.getX() - selectedX);i++){
			//	zisti ci sa tam nachadza nejaky pesiak
			if(getPiece(selectedPiece.getX()+(smerX*i), selectedPiece.getY()+(smerY*i))!= null){
				//	zisti ci sa na predoslom policku nenachadza fugurka alebo ci tam nieje hracova figurka (abz ju nepreskocil)
				if(!predosli && (selectedPiece.isWhite() != getPiece(selectedPiece.getX()+(smerX*i), selectedPiece.getY()+(smerY*i)).isWhite())){
					p = getPiece(selectedPiece.getX()+(smerX*i), selectedPiece.getY()+(smerY*i));
					predosli = true;
				}else{
					return null;
				}
			}else{
				predosli = false;
			}
		}
		if(p == null){
			p = selectedPiece;
		}
			return p;
	}

	public int getSelectedX() {
		return selectedX;
	}

	public void setSelectedX(int selectedX) {
		this.selectedX = selectedX;
	}

	public int getSelectedY() {
		return selectedY;
	}

	public void setSelectedY(int selectedY) {
		this.selectedY = selectedY;
	}

	public ArrayList<Piece> getPieces() {
		return pieces;
	}

	public void setPieces(ArrayList<Piece> pieces) {
		this.pieces = pieces;
	}

	public static void main(String[] args) {
		Checkers game = new Checkers();
		game.init();
	}

	public Piece getPiece(int x, int y) {
		for (Piece piece : pieces) {
			if (piece.getX() == x && piece.getY() == y)
				return piece;
		}
		return null;
	}
}
